package com.shijie.controller.erp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.core.annotation.Log;
import com.shijie.core.annotation.RequiresPermissions;
import com.shijie.core.constants.PnConstant.BusinessType;
import com.shijie.domain.entity.Brands;
import com.shijie.service.BrandsService;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

/*
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@RestController
@RequestMapping("/erp/brands")
public class BrandsController {

	@Resource
	private BrandsService brandsService;

	@GetMapping
	@RequiresPermissions(value={"brands:list","system:user:create","system:user:edit"})
	public Result list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize,
			@RequestParam(required = false) String q) {
		PageHelper.startPage(page, pageSize);
		List<Brands> list = brandsService.list(q, null);
		PageInfo<Brands> pageInfo = new PageInfo<>(list);
		return ResultGenerator.success(pageInfo);
	}
	
	@RequiresPermissions(value={"brands:create","brands:edit"})
	@Log(title = "品牌管理", action = BusinessType.INSERT)
	@PostMapping
	public Result add(@RequestBody Brands brands) {
		brandsService.save(brands);
		return ResultGenerator.success();
	}

	@RequiresPermissions(value={"brands:delete"})
	@Log(title = "品牌管理", action = BusinessType.DELETE)
	@DeleteMapping("/{id}")
	public Result delete(@PathVariable Integer id) {
		brandsService.deleteById(id);
		return ResultGenerator.success();
	}
	@GetMapping("/{id}")
	public Result detail(@PathVariable Integer id) {
		Brands brands = brandsService.selectById(id);
		return ResultGenerator.success(brands);
	}
}

